package com.tsll.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsll.dao.BookMapper;
import com.tsll.dao.VerifyBookMapper;
import com.tsll.entity.Book;
import com.tsll.entity.VerifyBook;
import com.tsll.service.BookService;
import com.tsll.service.VerifyBookService;
import com.tsll.vo.Result;
import com.tsll.vo.VerifyBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Tian
 * @version: 1.0
 * @since: 1.0
 * @date: 2024/3/28/8:59
 */
@Service
public class VerifyBookServiceImpl implements VerifyBookService {

    /**
     * 批量添加校验申请书籍
     * @param verifyBookVos
     * @param id
     * @return
     */
    @Autowired
    VerifyBookMapper verifyBookMapper;

    @Autowired
    BookMapper bookMapper;

    @Autowired
    BookService bookServiceImpl;




    @Override
    public Result batchAdd(List<VerifyBookVo> verifyBookVos, Integer id) {
        verifyBookMapper.batchAdd(verifyBookVos,id);
        return new Result(200,"成功添加校验申请信息",null);
    }

    /**
     * 根据校验id查询校验的书籍
     * @param verifyApplicationId 校验id
     * @return
     */
    @Override
    @Transactional
    public Result selectByVerifyApplicationId(Integer current,  Integer pageSize,Integer verifyApplicationId) {
        PageHelper.startPage(current,pageSize);
        List<VerifyBook> verifyBooks = verifyBookMapper.selectByVerifyApplicationId(verifyApplicationId);
        List<Book> books=new ArrayList<>();
        for (VerifyBook verifyBook:verifyBooks){
            Book book = bookServiceImpl.selectById(verifyBook.getBookid());
            book.setVerifyCount(verifyBook.getVerifycount());
            book.setDamageCount(verifyBook.getDamagecount());
            books.add(book);
        }
        PageInfo<Book> verifyBookPageInfo = new PageInfo<>(books);
        return new Result(200,"成功查询校验图书信息",verifyBookPageInfo);
    }

    @Override
    public Result searchAll(Integer current, Integer pageSize) {
        PageHelper.startPage(current,pageSize);
        List<Book> books = verifyBookMapper.searchAll();
        PageInfo<Book> pageInfo = new PageInfo<>(books);
        return new Result(200,"查询成功",pageInfo);
    }

    /**
     * 修复损坏图书
     * @param verifyBookVos
     * @return
     */
    @Override
    public Result updateRepairCount(List<VerifyBookVo> verifyBookVos){
        for (VerifyBookVo v : verifyBookVos) {
            verifyBookMapper.updateRepairCount(v);
        }
        return new Result(200,"修复成功",null);
    }

    /***
     * 清除损坏的图书
     * @param verifyBookVos
     * @return
     */
    @Override
    @Transactional
    public Result updateClearCount(List<VerifyBookVo> verifyBookVos){
        for (VerifyBookVo v : verifyBookVos) {
            verifyBookMapper.updateClearCount(v);
            bookMapper.updateBookCount(v.getBookId(),v.getClearCount());
        }
        return new Result(200,"清除成功成功",null);
    }
}
